const router = require('koa-router')();
const cognito = require("../app/aws/cognito/index");

router.prefix('/api/v1/cognito');

router.get('/', function (ctx, next) {
  ctx.body = 'this is a cognito response!'
});

router.post('/user/create', async (ctx, next) => {
  console.log(ctx.headers);
  const params = ctx.request.body;
  try {
    let json = await cognito.createUser(params);
    console.log(json);
    ctx.response.status = 200;
    ctx.response.body = {
      data: json,
    };
  } catch (error) {
    ctx.response.status = 500;
    ctx.response.body = {
      data: error,
    };
  }
});

router.post('/user/update', async (ctx, next) => {
  console.log(ctx.headers);
  const params = ctx.request.body;
  try {
    let json = await cognito.updateUser(params);
    console.log(json);
    ctx.response.status = 200;
    ctx.response.body = {
      data: json,
    };
  } catch (error) {
    ctx.response.status = 500;
    ctx.response.body = {
      data: error,
    };
  }
});

router.post('/password/change', async (ctx, next) => {
  console.log(ctx.headers);
  const params = ctx.request.body;
  try {
    let json = await cognito.changePassword(params);
    console.log(json);
    ctx.response.status = 200;
    ctx.response.body = {
      data: json,
    };
  } catch (error) {
    ctx.response.status = 500;
    ctx.response.body = {
      data: error,
    };
  }
});

module.exports = router;